home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / AptUrl / AptUrl.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.4 KB  |  190 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import apt
  6. import apt_pkg
  7. import gettext
  8. import aptsources.distro as aptsources
  9. import Parser
  10. import Helpers
  11. from aptsources.sourceslist import SourcesList, is_mirror
  12. from optparse import OptionParser
  13. from gettext import gettext as _
  14. import os
  15. import os.path as os
  16. allow_new_repositories = False
  17. channelsdir = '/usr/share/app-install/channels'
  18. (RESULT_OK, RESULT_CANCELT, RESULT_ERROR, RESULT_BADARGS) = range(4)
  19.  
  20. class AptUrlController(object):
  21.     
  22.     def __init__(self, ui):
  23.         self.ui = ui
  24.  
  25.     
  26.     def enableSection(self, apturl):
  27.         added = False
  28.         sourceslist = SourcesList()
  29.         distro = aptsources.distro.get_distro()
  30.         distro.get_sources(sourceslist)
  31.         requested_components = []
  32.         for component in apturl.section:
  33.             if component not in distro.enabled_comps and not self.cache.has_key(apturl.package):
  34.                 requested_components.append(component)
  35.                 continue
  36.         
  37.         if not requested_components:
  38.             return RESULT_OK
  39.         if not self.ui.askEnableSections(apturl.section):
  40.             return RESULT_CANCELT
  41.         if not self.ui.doEnableSection(apturl.section):
  42.             self.ui.error(_("Enabling '%s' failed") % ', '.join(apturl.section))
  43.             return RESULT_ERROR
  44.         self.ui.doUpdate()
  45.         self.openCache()
  46.         return RESULT_OK
  47.  
  48.     
  49.     def enableChannel(self, apturl):
  50.         channel = os.path.basename(apturl.channel)
  51.         channelpath = '%s/%s.list' % (channelsdir, channel)
  52.         channelkey = '%s/%s.key' % (channelsdir, channel)
  53.         if not os.path.exists(channelpath):
  54.             self.ui.error(_("Unknown channel '%s'") % channel, _("The channel '%s' is not known") % channel)
  55.             return RESULT_ERROR
  56.         if not self.ui.askEnableChannel(apturl.channel):
  57.             return RESULT_CANCELT
  58.         if not self.ui.doEnableChannel(channelpath, channelkey):
  59.             self.ui.error(_("Enabling channel '%s' failed") % apturl.channel)
  60.             return RESULT_ERROR
  61.         self.ui.doUpdate()
  62.         self.openCache()
  63.         return RESULT_OK
  64.  
  65.     
  66.     def openCache(self):
  67.         
  68.         try:
  69.             self.cache = apt.Cache()
  70.         except SystemError:
  71.             strerr = None
  72.             if '/etc/apt/sources.list' not in str(strerr):
  73.                 raise 
  74.             '/etc/apt/sources.list' not in str(strerr)
  75.             self.ui.error(_('Invalid /etc/apt/sources.list file'), strerr)
  76.             return False
  77.  
  78.         if self.cache._depcache.BrokenCount > 0:
  79.             err_header = _('Software index is broken')
  80.             err_body = _("This is a major failure of your software management system. Please check for broken packages with synaptic, check the file permissions and correctness of the file '/etc/apt/sources.list' and reload the software information with: 'sudo apt-get update' and 'sudo apt-get install -f'.")
  81.             self.ui.error(err_header, err_body)
  82.             return False
  83.         return True
  84.  
  85.     
  86.     def parseArgs(self):
  87.         parser = OptionParser()
  88.         parser.add_option('-p', '--http-proxy', dest = 'http_proxy', default = None, help = 'use http proxy')
  89.         (options, args) = parser.parse_args()
  90.         if options.http_proxy is not None:
  91.             proxy = options.http_proxy
  92.             if ':' not in proxy:
  93.                 proxy += ':3128'
  94.             
  95.             os.environ['http_proxy'] = 'http://%s' % proxy
  96.         
  97.         
  98.         try:
  99.             apturl_list = Parser.parse(args[0])
  100.         except IndexError:
  101.             e = None
  102.             self.ui.error(_('Need a url to continue, exiting'))
  103.             return []
  104.             except Parser.InvalidUrlException:
  105.                 e = None
  106.                 self.ui.error(_("Invalid url: '%s' given, exiting") % sys.argv[1], '%s' % e)
  107.                 return []
  108.             else:
  109.                 return apturl_list
  110.  
  111.  
  112.     
  113.     def verifyInstall(self, apturl):
  114.         ''' verify that the install package actually is installed '''
  115.         self.openCache()
  116.         pkg = self.cache[apturl.package]
  117.         if not (pkg.isInstalled) and pkg._pkg.CurrentState != apt_pkg.CurStateInstalled or self.cache._depcache.BrokenCount > 0:
  118.             return False
  119.         return True
  120.  
  121.     
  122.     def main(self):
  123.         ret = RESULT_OK
  124.         ui = self.ui
  125.         apturl_list = self.parseArgs()
  126.         if not apturl_list:
  127.             return RESULT_BADARGS
  128.         if not self.openCache():
  129.             return RESULT_ERROR
  130.         for apturl in apturl_list:
  131.             if apturl.section:
  132.                 if self.enableSection(apturl) != RESULT_OK:
  133.                     continue
  134.                 
  135.             elif apturl.channel:
  136.                 if self.enableChannel(apturl) != RESULT_OK:
  137.                     continue
  138.                 
  139.             elif apturl.refresh is not None:
  140.                 ui.doUpdate()
  141.                 if not self.openCache():
  142.                     return RESULT_ERROR
  143.             
  144.             if not self.cache.has_key(apturl.package):
  145.                 
  146.                 try:
  147.                     package_in_cache = bool(self.cache._cache[apturl.package])
  148.                 except KeyError:
  149.                     package_in_cache = False
  150.  
  151.                 if package_in_cache:
  152.                     ui.error(_("Package '%s' is virtual.") % apturl.package)
  153.                     continue
  154.                 else:
  155.                     ui.error(_("Could not find package '%s'.") % apturl.package)
  156.             
  157.             if self.cache[apturl.package].isInstalled and apturl.minver is None:
  158.                 ui.message(_("Package '%s' is already installed") % apturl.package)
  159.                 continue
  160.             
  161.             pkg = self.cache[apturl.package]
  162.             (sum, desc, homepage) = Helpers.parse_pkg(pkg)
  163.             if not ui.askInstallPackage(apturl.package, sum, desc, homepage):
  164.                 ret = RESULT_CANCELT
  165.                 continue
  166.             
  167.             
  168.             try:
  169.                 self.cache[apturl.package].markInstall()
  170.             except SystemError:
  171.                 e = None
  172.                 ui.error(_("Can not install '%s' (%s) ") % (apturl.package, e))
  173.                 continue
  174.  
  175.             if apturl.minver is not None:
  176.                 verStr = self.cache[apturl.package].candidateVersion
  177.                 if apt_pkg.VersionCompare(verStr, apturl.minver) < 1:
  178.                     ui.error(_("Package '%s' requests minimal version '%s', but only '%s' is available") % (apturl.package, apturl.minver, verStr))
  179.                     continue
  180.                 
  181.             
  182.             ui.doInstall(apturl)
  183.             if not self.verifyInstall(apturl):
  184.                 ret = RESULT_ERROR
  185.                 continue
  186.         
  187.         return ret
  188.  
  189.  
  190.